#!/usr/bin/env python
'''
Created on Mar 10, 2010

@author: abdelhamid
'''
from PyQt4 import QtCore, QtGui
from sys import *

class Ui_Form(QtGui.QTabWidget):
    def __init__(self, parent = None):
        super(Ui_Form, self).__init__(parent)
        self.setupUi(self)
        
    def setupUi(self, TabWidget):
        TabWidget.setObjectName("TabWidget")
        TabWidget.resize(631, 625)
        self.tab = QtGui.QWidget()
        self.tab.setObjectName("tab")
        self.gridLayout_3 = QtGui.QGridLayout(self.tab)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.groupBox = QtGui.QGroupBox(self.tab)
        self.groupBox.setObjectName("groupBox")
        self.gridLayout = QtGui.QGridLayout(self.groupBox)
        self.gridLayout.setObjectName("gridLayout")
        self.label = QtGui.QLabel(self.groupBox)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        self.lineEdit = QtGui.QLineEdit(self.groupBox)
        self.lineEdit.setObjectName("lineEdit")
        self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
        self.pushButton = QtGui.QPushButton(self.groupBox)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 2, 1, 1)
        self.pushButton_2 = QtGui.QPushButton(self.groupBox)
        self.pushButton_2.setObjectName("pushButton_2")
        self.gridLayout.addWidget(self.pushButton_2, 1, 2, 1, 1)
        self.gridLayout_3.addWidget(self.groupBox, 0, 0, 1, 1)
        self.groupBox_2 = QtGui.QGroupBox(self.tab)
        self.groupBox_2.setObjectName("groupBox_2")
        self.gridLayout_2 = QtGui.QGridLayout(self.groupBox_2)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.tableWidget = QtGui.QTableWidget(self.groupBox_2)
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(2)
        self.tableWidget.setRowCount(0)
        item = QtGui.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        item = QtGui.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(1, item)
        self.gridLayout_2.addWidget(self.tableWidget, 0, 0, 1, 1)
        self.tableWidget_2 = QtGui.QTableWidget(self.groupBox_2)
        self.tableWidget_2.setObjectName("tableWidget_2")
        self.tableWidget_2.setColumnCount(2)
        self.tableWidget_2.setRowCount(0)
        item = QtGui.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(0, item)
        item = QtGui.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(1, item)
        self.gridLayout_2.addWidget(self.tableWidget_2, 0, 1, 1, 1)
        self.tableWidget_3 = QtGui.QTableWidget(self.groupBox_2)
        self.tableWidget_3.setObjectName("tableWidget_3")
        self.tableWidget_3.setColumnCount(2)
        self.tableWidget_3.setRowCount(0)
        item = QtGui.QTableWidgetItem()
        self.tableWidget_3.setHorizontalHeaderItem(0, item)
        item = QtGui.QTableWidgetItem()
        self.tableWidget_3.setHorizontalHeaderItem(1, item)
        self.gridLayout_2.addWidget(self.tableWidget_3, 1, 0, 1, 1)
        self.tableWidget_4 = QtGui.QTableWidget(self.groupBox_2)
        self.tableWidget_4.setObjectName("tableWidget_4")
        self.tableWidget_4.setColumnCount(2)
        self.tableWidget_4.setRowCount(0)
        item = QtGui.QTableWidgetItem()
        self.tableWidget_4.setHorizontalHeaderItem(0, item)
        item = QtGui.QTableWidgetItem()
        self.tableWidget_4.setHorizontalHeaderItem(1, item)
        self.gridLayout_2.addWidget(self.tableWidget_4, 1, 1, 1, 1)
        self.gridLayout_3.addWidget(self.groupBox_2, 1, 0, 1, 1)
        TabWidget.addTab(self.tab, "")

        self.retranslateUi(TabWidget)
        TabWidget.setCurrentIndex(0)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.BrowseFile)
        QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL("clicked()"), self.LexCheck)
        QtCore.QMetaObject.connectSlotsByName(TabWidget)

    def retranslateUi(self, TabWidget):
        TabWidget.setWindowTitle(QtGui.QApplication.translate("TabWidget", "PLex File Check", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox.setTitle(QtGui.QApplication.translate("TabWidget", "Choose File", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("TabWidget", "File", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("TabWidget", "&Browse..", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_2.setText(QtGui.QApplication.translate("TabWidget", "&Check File", None, QtGui.QApplication.UnicodeUTF8))
        self.tableWidget.horizontalHeaderItem(0).setText(QtGui.QApplication.translate("TabWidget", "Lexim", None, QtGui.QApplication.UnicodeUTF8))
        self.tableWidget.horizontalHeaderItem(1).setText(QtGui.QApplication.translate("TabWidget", "Type", None, QtGui.QApplication.UnicodeUTF8))
        self.tableWidget_2.horizontalHeaderItem(0).setText(QtGui.QApplication.translate("TabWidget", "Lexim", None, QtGui.QApplication.UnicodeUTF8))
        self.tableWidget_2.horizontalHeaderItem(1).setText(QtGui.QApplication.translate("TabWidget", "Type", None, QtGui.QApplication.UnicodeUTF8))
        self.tableWidget_3.horizontalHeaderItem(0).setText(QtGui.QApplication.translate("TabWidget", "Lexim", None, QtGui.QApplication.UnicodeUTF8))
        self.tableWidget_3.horizontalHeaderItem(1).setText(QtGui.QApplication.translate("TabWidget", "Type", None, QtGui.QApplication.UnicodeUTF8))
        self.tableWidget_4.horizontalHeaderItem(0).setText(QtGui.QApplication.translate("TabWidget", "Lexim", None, QtGui.QApplication.UnicodeUTF8))
        self.tableWidget_4.horizontalHeaderItem(1).setText(QtGui.QApplication.translate("TabWidget", "Type", None, QtGui.QApplication.UnicodeUTF8))
        TabWidget.setTabText(TabWidget.indexOf(self.tab), QtGui.QApplication.translate("TabWidget", "File Lex Check", None, QtGui.QApplication.UnicodeUTF8))
    
    
    #############################################################
    #########################Scanning############################
    #############################################################
    def scanWhiteSpace(self, scannedText):
        if (scannedText == " ") or (scannedText == "") or (scannedText == "\n") or (scannedText == "\t"):
            return True
        else:
            return False

    def scanLetter(self, scannedText):
        if (scannedText >= 'A' and scannedText <= 'Z') or (scannedText >= 'a' and scannedText <= 'z') or (scannedText == '_'):
            return True
        else:
            return False
            
    def scanDigits(self, scannedText):
        if (scannedText >= '0' and scannedText <= '9'):
            return True
        else:
            return False
        
    def scanOperators(self, scannedText):
        if (scannedText == '+') or (scannedText == '-') or (scannedText =='*') or (scannedText == '='):
            return True
        else:
            return False         

    #####################################################################
    ############################GetTokens################################
    #####################################################################
    def getIdTokens(self, scannedText):
        buffStr=""
        TokensList=[]
        #state=0
        for i in scannedText:
            if self.scanLetter(i) == True:
                buffStr+=i
                #state=1
            elif (self.scanDigits(i) == True) and (self.scanLetter(buffStr) == True):
                buffStr+=i
            
            else:
                if (self.scanWhiteSpace(buffStr) == True):
                    continue
                TokensList.append((buffStr,'Variable'))
                buffStr=""    
        return TokensList
    
    def getDigitsTokens(self, scannedText):
        """
        TODO: Fix one problem
            * if the token contains any non digit string stransform it to invalid token
        """
        buffStr=""
        TokensList=[]
        for i in scannedText:
            if self.scanDigits(i) == True:
                buffStr+=i
            else:
                if (self.scanWhiteSpace(buffStr) == True):
                    continue
                TokensList.append((buffStr,'Digit'))
                buffStr=""
        return TokensList
    
    
    def getOperatorsTokens(self, scannedText):
        """
        Done!
        """
        buffStr=""
        TokensList=[]
        for i in scannedText:
            if self.scanOperators(i) == True:
                buffStr=i
                if (self.scanWhiteSpace(buffStr) == True):
                    continue
                TokensList.append((buffStr,'Operator'))
            else:
                buffStr=""
        return TokensList
    
    
    
    
    def getInvalidTokens(self, scannedText):
        """
        Done!
        """
        buffStr=""
        TokensList=[]
        for i in scannedText:
            if (self.scanLetter(i) == False) and (self.scanDigits(i) == False) and (self.scanOperators(i) == False):
                buffStr=i
                if (self.scanWhiteSpace(buffStr) == True):
                    continue
                TokensList.append((buffStr,'Invalid Token'))
            else:
                buffStr=""
        return TokensList
    
    def BrowseFile(self):
        lexFile = unicode(QtGui.QFileDialog.getOpenFileName(self, 'Open File...', '/home/abdelhamid/Desktop/'))
        self.lineEdit.setText(lexFile)
    
    def LexCheck(self):
        plainFile=unicode(self.lineEdit.text())
        openedFile=open(plainFile,'r+')
        stream=openedFile.read()
        out1=self.getIdTokens(stream)
        out2=self.getDigitsTokens(stream)
        out3=self.getOperatorsTokens(stream)
        out4=self.getInvalidTokens(stream)
        
        self.tableWidget.setRowCount(len(out1))        
        for i in range(len(out1)):
            item = QtGui.QTableWidgetItem(out1[i][0])
            item2 = QtGui.QTableWidgetItem(out1[i][1])
            self.tableWidget.setItem(i,0,item)       
            self.tableWidget.setItem(i,1,item2)
        
        
        self.tableWidget_2.setRowCount(len(out2))        
        for i in range(len(out2)):
            item = QtGui.QTableWidgetItem(out2[i][0])
            item2 = QtGui.QTableWidgetItem(out2[i][1])
            self.tableWidget_2.setItem(i,0,item)       
            self.tableWidget_2.setItem(i,1,item2)
            
        
        self.tableWidget_3.setRowCount(len(out3))        
        for i in range(len(out3)):
            item = QtGui.QTableWidgetItem(out3[i][0])
            item2 = QtGui.QTableWidgetItem(out3[i][1])
            self.tableWidget_3.setItem(i,0,item)       
            self.tableWidget_3.setItem(i,1,item2)
            
        self.tableWidget_4.setRowCount(len(out4))        
        for i in range(len(out4)):
            item = QtGui.QTableWidgetItem(out4[i][0])
            item2 = QtGui.QTableWidgetItem(out4[i][1])
            self.tableWidget_4.setItem(i,0,item)       
            self.tableWidget_4.setItem(i,1,item2)

if __name__ == '__main__':
    app = QtGui.QApplication(argv)
    abstract_window = Ui_Form()
    abstract_window.show()
    app.exec_()